package com.tear.language.node;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;

import net.sf.json.JSONObject;
import ognl.Ognl;

import org.apache.commons.lang.StringUtils;

import com.tear.language.utils.Attrs;
import com.tear.language.utils.TypeUtil;

/**

	循环操作
	属性如下
	collection 遍历的集合或map，可不填单纯依靠condition做简单循环
	condition 遍历条件，可不填，单纯遍历collection
	break 循环退出条件支持ognl
	continue 循环跳过条件，支持ognl
	index 循环变量可以是集合下标，也可以是map的key
	var 循环中获取集合或者map中的值保存在这个对应的变量中
*/
public class WhileNode extends BaseNode {

	@Override
	public boolean parse(Map<String, Object> currParams,Map<String, Object> globalParams, JSONObject obj) throws Exception {
		String conditionStr = obj.optString(Attrs.CONDITION);
		String breakStr = obj.optString(Attrs.BREAK);
		String continueStr = obj.optString(Attrs.CONTINUE);
		String index = obj.optString(Attrs.INDEX);
		String varStr = obj.optString(Attrs.VAR);
		String collectionStr = obj.optString(Attrs.COLLECTION);
		if(index != null) {
			if(currParams.get(index) != null) {
				currParams.put(index+"_", (Integer)currParams.get(index+"_") + 1);
			} else {
				currParams.put(index+"_", 0);
			}
			currParams.put(index, (Integer)currParams.get(index+"_"));
		}
		boolean condition = true;
		boolean break_ = false;
		boolean continue_ = false;
		Map<String, Object> allParams = getAllParams(currParams,globalParams);
		Object collection = null;
		if(StringUtils.isNotEmpty(collectionStr)) {
			collection = Ognl.getValue(collectionStr,allParams);
			//如果集合属性不为空，但是条件为null则默认加上一个边界条件
			if(StringUtils.isEmpty(conditionStr)) {
				if(collection instanceof List) {
					conditionStr = index+"_<"+collectionStr+".size()";
				} else if(collection instanceof Map){
					Map map = (Map)collection;
					Set set = map.entrySet();
					List list = new ArrayList(set);
					allParams.put("_list_", list);
					conditionStr = index+"_<_list_"+".size()";
				}
			}
		}
		if(StringUtils.isNotEmpty(conditionStr)) {
			conditionStr = TypeUtil.replaceArray(conditionStr);
			condition = (Boolean)Ognl.getValue(conditionStr,allParams);
		}
		if(StringUtils.isNotEmpty(breakStr)) {
			breakStr = TypeUtil.replaceArray(breakStr);
			break_ = (Boolean)Ognl.getValue(breakStr,allParams);
		}
		if(StringUtils.isNotEmpty(continueStr)) {
			continueStr = TypeUtil.replaceArray(continueStr);
			continue_ = (Boolean)Ognl.getValue(continueStr,allParams);
		}
		boolean flag = true;
		currParams.put(Attrs.WHILE_INDEX, index);
		currParams.put(Attrs.WHILE_FLAG, "true");
		if(continue_) {
			flag = false;
		} else if(break_) {
			flag = false;
			destroyVars(currParams, index, varStr);
		} else if(condition) {
			try {
				if(StringUtils.isNotEmpty(varStr) && StringUtils.isNotEmpty(collectionStr)) {
					Object value = null;
					int idx = Integer.parseInt(currParams.get(index+"_").toString());
					if(collection instanceof List) {
						value = ((List)collection).get(idx);
						currParams.put(varStr, value);
					} else if(collection instanceof Map){
						Map map = (Map)collection;
						Set<Entry<String,Object>> set = map.entrySet();
						List<Entry<String,Object>> list = new ArrayList(set);
						currParams.put(varStr, list.get(idx).getValue());
						currParams.put(index, list.get(idx).getKey());
					}
				
				}
			} catch (Exception e) {
				throw new Exception("从集合或者映射取值"+currParams.get(index)+"错误"+e.getMessage());
			}
			
		} else {
			flag = false;
			destroyVars(currParams, index, varStr);
		}
	
		return flag;
		
	}

	//释放临时变量
	private void destroyVars(Map<String, Object> currParams, String index,
			String varStr) {
		currParams.remove(Attrs.WHILE_INDEX);
		currParams.remove(Attrs.WHILE_FLAG);
		currParams.remove(varStr);
		currParams.remove(index);
		currParams.remove(index+"_");
	}

}
